package com.sdd.rpi_mobile.infoTypes;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.MenuItem;
import android.widget.TextView;

import com.sdd.rpi_mobile.R;
import com.sdd.rpi_mobile.homeScreen.GridChooser;

/** Displays and controls the information for the Useful Information feature. */
public class InformationActivity extends Activity {
	
	private TextView mTitle, mDescription, mLink;
	
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.useful_info_template);
		
		// Enable the Home button in the action bar.
		if (Build.VERSION.SDK_INT >= 14) {
			getActionBar().setHomeButtonEnabled(true);
		}
		
		Bundle bundle = this.getIntent().getExtras();
		int infoId = bundle.getInt("resource");
		
		//Get the resource that was selected item e.g. Health Insurance.
		Resources res = getResources();
		String[] info = res.getStringArray(infoId);
		
		UsefulInfo infoObj = new UsefulInfo(info);
		
		//Setup the title.
		this.mTitle = (TextView) findViewById(R.id.header);
		this.mTitle.setText(infoObj.getName());

		//Setup the description.
		this.mDescription = (TextView) findViewById(R.id.description);
		this.mDescription.setText(infoObj.getDescription());
		
		//Setup the link.
		this.mLink = (TextView) findViewById(R.id.info_link);
		this.mLink.setText(infoObj.getLink());
		Linkify.addLinks(mLink, Linkify.ALL);
		Linkify.addLinks(this.mDescription, Linkify.ALL);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case android.R.id.home:
		    	// App icon in action bar clicked; go home
	            Intent intent = new Intent(this, GridChooser.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
